﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using greenwerkz.constructs;




namespace greenwerkz.webservices
{
    /// <summary>
    /// Summary description for patients
    /// </summary>
    public class patients : System.Web.Services.WebService
    {
        public patients()
        {

            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }

        [WebMethod]
        public test HelloWorld()
        {
            test l_test = new test();
            l_test.Test1 = "chad";
            l_test.Test2 = "Jennewine";
            return l_test;
        }

        [WebMethod]
        private GWpatient[] FindPatientByLnameFname(string lName, string fName)
        {
            return GWpatientFuncs.CachedPatients.Where(s => s.PatientLastName.ToLower().StartsWith(lName)).ToArray<GWpatient>();
        }
        [WebMethod]
        private GWpatient FindPatientByID(int l_patientID)
        {
            return GWpatientFuncs.CachedPatients.First(s => s.MemberID == l_patientID);
        }

        [WebMethod]
        private List<GWpatient> SelectPatientAll()
        {
            return GWpatientFuncs.CachedPatients;
        }
        [WebMethod]
        private bool PatientExistsByLoginID(string l_loginID)
        {
            return (GWpatientFuncs.CachedPatients.Where(s => s.MemberUserName.ToLower().Equals(l_loginID.ToLower())).Count() > 0);
        }
        [WebMethod]
        private List<string> FindPatientByCaregiver(string sCaregiver)
        {
            List<string> sReturn = new List<string>();
            foreach (GWpatient child in GWpatientFuncs.CachedPatients.Where(s => s.PatientLastName.ToLower().StartsWith(sCaregiver)))
            {
                sReturn.Add(child.PatientLastName);
            }
            return sReturn;

        }
        [WebMethod]
        private List<string> FindPatientByLname(string lName)
        {
            List<string> sReturn = new List<string>();
            foreach (GWpatient child in GWpatientFuncs.CachedPatients.Where(s => s.PatientLastName.ToLower().StartsWith(lName)))
            {
                sReturn.Add(child.PatientLastName);
            }
            return sReturn;
        }
        [WebMethod]
        private GWpatient selectPatientForPlant(bool l_isVeg, int l_growOperationID)
        {
            GWpatient l_returnPatient = GWpatientFuncs.selectPatientForPlant(l_isVeg, l_growOperationID);
            return l_returnPatient;
        }

        [WebMethod]
        public int InsertUpdatePatient(GWpatient l_patient)
        {
            return GWpatientFuncs.insertUpdatePatient(l_patient);
        }

        [WebMethod]
        public GWpatient loginUser (string l_userName, string l_password)
        {
            if (Membership.ValidateUser(l_userName, l_password))
            {
                FormsAuthentication.SetAuthCookie(l_userName, true);
                //redirect back to where they came from
                return GWpatientFuncs.buildPatient(
                    (int) Membership.GetUser(l_userName).ProviderUserKey 
                    );
            }
            return new GWpatient();

        }

    }
}